/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-03-01 11:01:58
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-03-14 15:16:22
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/utils/device_func_util.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:collection/collection.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/data_type_config.dart';
import 'package:yt_devops/pages/devices/device_tab/model/device_item_model.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/utils/func_utils.dart';

class DeviceFuncUtils {
  /// 通过属性值的dataType来获取其值的格式化方法
  static Future<List<List<DetailItemModel>>> getFormateByDeviceModel(
      List<List<DetailItemModel>> groups, int? productId) async {
    await Configs.httpClient.requestWithLoading(() async {
      var model =
          await DeviceServices().getDeviceProperty({'productId': productId});
      List properties = model['properties'];
      for (var group in groups) {
        for (var item in group) {
          var property = properties
              .firstWhereOrNull((element) => element['id'] == item.field);
          if (property != null) {
            var dataTypeModel = DataTypeConfig.fromJson(property['dataType']);
            item.setFormat =
                (value) => FuncUtils.formatModelValue(value, dataTypeModel);
          }
        }
      }
    });
    return groups;
  }
}
